package com.woniu.woniu_bx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.woniu_bx.pojo.Chat;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @ClassName ChatMapper
 * @Description 聊天记录 Mapper接口
 * @Author 魏才林
 * @Date 2022/8/27 16:27
 * @Version 1.0
 **/
public interface ChatMapper extends BaseMapper<Chat> {

    @Select("select * from bx_chat where (send_id=#{myId} and accept_id=#{otherId}) or (send_id=#{otherId} and accept_id=#{myId}) order by time desc")
    List<Chat> findChatList(@Param("myId") int myId, @Param("otherId") int otherId);

    /**
     * 根据用户查找所有的接受id
     * @param id
     * @return
     */
    @Select("SELECT DISTINCT accept_id FROM bx_chat WHERE send_id=#{id}")
    List<Integer> getChatList(int id);

    /**
     * 根据发送人和收信人的id查询未读的条数
     * @param sendId
     * @param acceptId
     * @return
     */
    @Select("SELECT COUNT(*) FROM bx_chat WHERE accept_id= #{acceptId} AND send_id = #{sendId} AND readed = 'n';")
    Integer sumUnReadChat(@Param("sendId") int sendId, @Param("acceptId") int acceptId);

    /**
     * 查找未读的信息
     * @param myId
     * @param otherId
     * @return
     */
    @Select("select * from bx_chat where readed = 'n' and ((send_id=#{myId} and accept_id=#{otherId}) or (send_id=#{otherId} and accept_id=#{myId})) ")
    List<Chat> findChatListUnRead(@Param("myId") int myId, @Param("otherId") int otherId);
}
